<template>
  <div class="tradeMark-container">
    <!-- 按钮 -->
    <el-button type="primary" icon="el-icon-plus" @click="dialogFormVisible = true">添加</el-button>
    <!-- 表单 -->
    <el-table :data="list" style="width: 100%; margin: 20px 0" border>
      <el-table-column type="index" label="序号" width="80%" align="center" />
      <el-table-column prop="tmName" label="品牌名称" width="width" />
      <el-table-column prop="logoUrl" label="品牌LOGO" width="width">
        <template slot-scope="{ row }">
          <img
            v-image="require('@/assets/aa.jpeg')"
            :src="row.logoUrl"
            alt=""
            style="width: 100px; height: 100px"
          >
        </template>
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{row}">
          <el-button
            type="warning"
            icon="el-icon-edit"
            size="mini"
            @click="updateTradeMark(row)"
          >修改</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="delTradeMark(row.id)"
          >删除</el-button>
        </template>

      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      style="textAlign: center"
      layout="prev, pager, next, jumper, ->,sizes, total"
      :total="total"
      :current-page="page"
      :page-size="limit"
      :page-sizes="[3, 5, 10]"
      :pager-count="7"
      @current-change="getTradeMarkList"
      @size-change="changeSize"
    />
    <!-- 对话框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancellFn">
      <el-form ref="tmForm" :model="tmForm" :rules="rules">
        <el-form-item label="品牌名字" prop="tmName">
          <el-input v-model="tmForm.tmName" autocomplete="off" style="width:80%" />
        </el-form-item>
        <el-form-item label="品牌LOGO" prop="logoUrl">
          <!-- 上传图片 -->
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="textAlign:center">
        <el-button @click="cancellFn">取 消</el-button>
        <el-button
          type="primary"
          @click="addTradeMark"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      page: 1,
      limit: 3,
      total: 0,
      list: [],
      dialogFormVisible: false,
      tmForm: {
        tmName: '',
        logoUrl: '',
        id: ''
      },
      rules: {
        tmName: [
          { required: true, message: '请输入品牌名称', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'change' }
        ],
        logoUrl: [
          { required: true, message: '请上传品牌LOGO' }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.tmForm.id ? '修改品牌' : '添加品牌'
    }
  },
  mounted() {
    this.getTradeMarkList()
  },
  methods: {
    // 获取品牌数据
    async getTradeMarkList(pages = 1) {
      this.page = pages
      const { page, limit } = this
      const res = await this.$API.trademark.reqTradeMarkList(page, limit)
      this.total = res.data.total
      this.list = res.data.records
    },
    // 每页条数改变时
    changeSize(index) {
      this.limit = index
      this.getTradeMarkList()
    },
    // 上传成功
    handleAvatarSuccess(res, file) {
      this.tmForm.logoUrl = res.data
    },
    // 上传前
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 添加品牌
    async addTradeMark() {
      await this.$refs.tmForm.validate()
      await this.$API.trademark.reqAddOrUpdateTradeMark(this.tmForm)
      this.dialogFormVisible = false
      this.$message.success('添加成功')
      this.getTradeMarkList(this.tmForm.id ? this.page : 1)
    },
    // 取消按钮
    cancellFn() {
      this.dialogFormVisible = false
      this.$refs.tmForm.resetFields()
      this.tmForm.tmName = ''
      this.tmForm.logoUrl = ''
      this.tmForm.id = ''
    },
    // 修改品牌
    updateTradeMark(row) {
      this.dialogFormVisible = true
      this.tmForm = { ...row }
    },
    // 删除数据
    async delTradeMark(id) {
      await this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await this.$API.trademark.reqDeleteTradeMark(id)
      this.$message.success('删除成功')
      this.getTradeMarkList(this.list.length > 1 ? this.page : this.page - 1)
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
